<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="utf-8" lang="utf-8">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>
   验证码辅助函数 - CodeIgniter 中文手册|用户手册|用户指南|Wiki文档
  </title>
  <link rel="shortcut icon" href="/user_guide/../images/design/favicon.ico" type="image/x-icon" />
  <link rel="stylesheet" type="text/css" media="all" href="/user_guide/userguide.css" />
  <link rel="search" href="/user_guide/../CodeIgniterSearch.xml" type="application/opensearchdescription+xml" title="CodeIgniter 搜索" />
  <link rel="canonical" href="/user_guide/helpers/captcha_helper.html" />
  <script type="text/javascript" src="/user_guide/nav/nav.js">
  </script>
  <script type="text/javascript" src="/user_guide/nav/prototype.lite.js">
  </script>
  <script type="text/javascript" src="/user_guide/nav/moo.fx.js">
  </script>
  <script type="text/javascript" src="/user_guide/nav/user_guide_menu.js">
  </script>
  <meta name="robots" content="all" />
  <meta name="author" content="ExpressionEngine Dev Team" />
  <meta name="description" content="CodeIgniter 中文手册, CodeIgniter 用户指南, CodeIgniter User Guide, Wiki 文档" />
 </head>
 <body>
  <!-- START NAVIGATION -->
  <div id="nav">
   <div id="nav_inner">
    <script type="text/javascript">
     create_menu('/user_guide/');
    </script>
   </div>
  </div>
  <script type="text/javascript">
   _setNavigation();
  </script>
  <div id="nav2">
   <a name="top">
   </a>
   <a href="javascript:void(0);" onclick="myHeight.toggle();">
    <img src="/user_guide/images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="切换目录" alt="切换目录" />
   </a>
  </div>
  <div id="masthead">
   <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
    <tr>
     <td width="350">
      <h1>
       CodeIgniter 用户指南 版本 2.0.0
      </h1>
     </td>
     
     <td id="breadcrumb_right">
      <a href="/user_guide/toc.html">
       目录页
      </a>
     </td>
    </tr>
   </table>
  </div>
  <!-- END NAVIGATION -->
  <!-- START BREADCRUMB -->
  <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
   <tr>
    <td id="breadcrumb">
     <a href="/" target="_blank">
      CodeIgniter 中国首页
     </a>
     &nbsp;&#8250;&nbsp;
     <a href="/user_guide/toc.html">用户指南目录</a>
     &nbsp;&#8250;&nbsp;验证码辅助函数
    </td>
    <td id="searchbox">
     <form method="get" action="http://www.google.com/search" target="google_window">
      <input type="hidden" name="client" value="pub-0176846097796333" />
      <input type="hidden" name="forid" value="1" />
      <input type="hidden" name="ie" value="UTF-8" />
      <input type="hidden" name="oe" value="UTF-8" />
      <input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.org.cn/user_guide/" />
      搜索用户指南&nbsp;
      <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />
      &nbsp;
      <input type="submit" class="submit" name="sa" value="Go" />
     </form>
    </td>
   </tr>
  </table>
  <!-- END BREADCRUMB -->
  <div style="clear:both;text-align:right;padding: 6px 40px 0 0;">
   <a href="http://codeigniter.com/user_guide/helpers/captcha_helper.html" target="_blank">
    查看原文
   </a>
  </div>
  <!--<br clear="all"  />-->
  <!-- START CONTENT -->
  <div id="content">
   <h1>
    CAPTCHA 辅助函数
   </h1>
   <p>
    验证码辅助函数用来生成图片验证码
   </p>
   <h2>
    加载辅助函数
   </h2>
   <p>
    用下面的代码加载验证码辅助函数:
   </p>
   <code>
    $this-&gt;load-&gt;helper('captcha');
   </code>
   <p>
    可用的函数如下：
   </p>
   <h2>
    captcha_create(
    <var>
     $data
    </var>
    )
   </h2>
   <p>
    根据你指定的一系列参数创建验证码图像, 返回值是一个包含此图像数据的数组.
   </p>
   <code>
    [array]
    <br />
    (
    <br />
    &nbsp;&nbsp;'image' => IMAGE TAG
    <br />
    &nbsp;&nbsp;'time'  => TIMESTAMP (毫秒)
    <br />
    &nbsp;&nbsp;'word'  => CAPTCHA WORD
    <br />
    )
   </code>
   <p>
    "image"是实际存在image标记:
    <code>
     &lt;img src=&quot;http://example.com/captcha/12345.jpg&quot; width=&quot;140&quot; height=&quot;50&quot; /&gt;
    </code>
   </p>
   <p>
    这里的"time"是一个毫秒级的时间戳，作为图片文件名（不包含扩展名）. 就像这样:  1139612155.3422
   </p>
   <p>
    "word"是验证码, 如果不提供, 将是一个随机字符串.
   </p>
   <h3>
    使用验证码辅助函数：
   </h3>
   <p>
    加载后你可以向这样产生一个验证码:
   </p>
   <code>
    $vals = array(
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;'word'     => 'Random word',
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;'img_path'   => './captcha/',
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;'img_url'   => 'http://example.com/captcha/',
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;'font_path'   => './path/to/fonts/texb.ttf',
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;'img_width'   => '150',
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;'img_height' => 30,
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;'expiration' => 7200
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;);
    <br />
    <br />
    $cap = create_captcha($vals);
    <br />
    echo $cap['image'];
   </code>
   <ul>
    <li>
     验证码辅助函数必须需要GD库.
    </li>
    <li>
     只有 img_path 和 img_url 参数是必须的.
    </li>
    <li>
     如果"word"未提供, 将自动产生一个ASCII字符串.  你也可以使用自己的词库，从里面随机挑选.
    </li>
    <li>
     如果未提供TRUE TYPE字体的路径, 将会使用GD自带的字体.
    </li>
    <li>
     "captcha" 目录必须可写(666, or 777)
    </li>
    <li>
     "expiration" (秒) 指定了验证码图片的超时删除时间.  默认是2小时.
    </li>
   </ul>
   <h3>
    配合数据库
   </h3>
   <p>
    为了在提交表单时用到验证，你需要将
    <kbd>
     create_captcha()
    </kbd>
    生成的结果保存到数据库。这样，当用户提交表单时，你就可以验证数据库里是否有此验证码或是否过期。
   </p>
   <p>
    这是一个数据表的例子:
   </p>
   <code>
    CREATE TABLE captcha (
    <br />
    &nbsp;captcha_id bigint(13) unsigned NOT NULL auto_increment,
    <br />
    &nbsp;captcha_time int(10) unsigned NOT NULL,
    <br />
    &nbsp;ip_address varchar(16) default '0' NOT NULL,
    <br />
    &nbsp;word varchar(20) NOT NULL,
    <br />
    &nbsp;PRIMARY KEY `captcha_id` (`captcha_id`),
    <br />
    &nbsp;KEY `word` (`word`)
    <br />
    );
   </code>
   <p>
    这是一个使用数据库的例子.  一个带验证码的页面显示如下:
   </p>
   <code>
    $this-&gt;load-&gt;helper(&#x27;captcha&#x27;);
    <br />
    $vals = array(
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;&#x27;img_path&#x27;   =&gt; &#x27;./captcha/&#x27;,
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;&#x27;img_url&#x27;   =&gt; &#x27;http://example.com/captcha/&#x27;
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;);
    <br />
    <br />
    $cap = create_captcha($vals);
    <br />
    <br />
    $data = array(
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;&#x27;captcha_time&#x27;  =&gt; $cap[&#x27;time&#x27;],
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;&#x27;ip_address&#x27;  =&gt; $this-&gt;input-&gt;ip_address(),
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;&#x27;word&#x27;      =&gt; $cap[&#x27;word&#x27;]
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;);
    <br />
    <br />
    $query = $this-&gt;db-&gt;insert_string(&#x27;captcha&#x27;, $data);
    <br />
    $this-&gt;db-&gt;query($query);
    <br />
    <br />
    echo &#x27;提交下面的验证码:&#x27;;
    <br />
    echo $cap[&#x27;image&#x27;];
    <br />
    echo &#x27;&lt;input type=&quot;text&quot; name=&quot;captcha&quot; value=&quot;&quot; /&gt;&#x27;;
   </code>
   <p>
    然后页面提交后如下处理:
   </p>
   <code>
    // 首先删除旧的验证码
    <br />
    $expiration = time()-7200; // 2小时限制
    <br />
    $this-&gt;db-&gt;query(&quot;DELETE FROM captcha WHERE captcha_time &lt; &quot;.$expiration);
    <br />
    <br />
    // 然后再看是否有验证码存在:
    <br />
    $sql = &quot;SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND date &gt; ?&quot;;
    <br />
    $binds = array($_POST[&#x27;captcha&#x27;], $this-&gt;input-&gt;ip_address(), $expiration);
    <br />
    $query = $this-&gt;db-&gt;query($sql, $binds);
    <br />
    $row = $query-&gt;row();
    <br />
    <br />
    if ($row-&gt;count == 0)
    <br />
    &#123;
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;echo &quot;你必须提交图像上显示的验证码&quot;;
    <br />
    &#125;
   </code>
   <p>
    &nbsp;
   </p>
   <div id="Contributors">
    翻译贡献者:
CosyBoy, Hex, LSvKing, 杜建宇
   </div>
   <div id="DocDate">
    最后修改: 2011-04-23 11:43:41
   </div>
  </div>
  <!-- END CONTENT -->
  <div id="footer">
   <p>
    上一个主题:&nbsp;&nbsp;
    <a href="/user_guide/helpers/url_helper.html">
     URL 辅助函数
    </a>
    &nbsp;&nbsp;&middot;&nbsp;&nbsp;
    <a href="#top">
     页首
    </a>
    &nbsp;&middot;&nbsp;&nbsp;
    <a href="/user_guide/toc.html">用户指南目录</a>
   </p>
   <p>
    <a href="http://codeigniter.com">
     CodeIgniter
    </a>
    &nbsp;&middot;&nbsp; 版权所有 &#169; 2006-2011 &nbsp;&middot;&nbsp;
    <a href="http://ellislab.com/">
     Ellislab, Inc.
    </a>
   </p>
   <p>
    中文化:
    <a href="">
     CodeIgniter 中国
    </a>
    &nbsp;&middot;&nbsp; 制作: Hex &nbsp;&middot;&nbsp; 版本: 1.20 &nbsp;&middot;&nbsp; 鸣谢: 子非鱼
   </p>
  </div>
  
  <div style="display:none">
   
   <noscript>
    <a href="http://www.51.la/?3289908" target="_blank">
     <img alt="我要啦免费统计" src="http://img.users.51.la/3289908.asp" style="border:none" />
    </a>
   </noscript>
  </div>
 </body>
</html>